//
//  ZombieView.m
//  UI5_UIControl课后作业
//
//  Created by dllo on 15/11/12.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZombieView.h"

@implementation ZombieView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor lightGrayColor];

    NSMutableArray *fileArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 22; i++) {
        NSString *fileName = [NSString stringWithFormat:@"Zombie%ld.tiff", i];
        UIImage *image = [UIImage imageNamed:fileName];
        [fileArr addObject:image];
    }
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.frame];
    imageV.tag = 10001;
    imageV.image = [UIImage imageNamed:@"Zombie0.tiff"];
    imageV.animationImages = fileArr;
    [self addSubview:imageV];
//    [imageV startAnimating];
    [imageV release];

    UIButton *startB = [UIButton buttonWithType:UIButtonTypeCustom];
    startB.frame = CGRectMake(30, 50, 50, 50);
    [startB setTitle:@"开始" forState:UIControlStateNormal];
    [startB addTarget:self action:@selector(startBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:startB];

    UIButton *stopB = [UIButton buttonWithType:UIButtonTypeCustom];
    stopB.frame = CGRectMake(30, 130, 50, 50);
    [stopB setTitle:@"停止" forState:UIControlStateNormal];
    [stopB addTarget:self action:@selector(stopBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:stopB];

    
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 600, 300, 30)];
    slider.maximumValue = 2.0;
    slider.minimumTrackTintColor = [UIColor blueColor];
    slider.maximumTrackTintColor = [UIColor redColor];
    slider.thumbTintColor = [UIColor yellowColor];
    
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:slider];
    [slider release];
    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(300, 30, 50, 50);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(returnButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    

}

- (void)returnButton:(UIButton *)sender
{
    [self removeFromSuperview];
}

- (void)sliderAction:(UISlider *)sender
{
    NSLog(@"%f", sender.value);
    UIImageView *view = (UIImageView *)[self viewWithTag:10001];
    view.animationDuration = sender.maximumValue - sender.value + 0.01;
    [view startAnimating];
}


- (void)startBAction:(UIButton *)sender
{
    UIImageView *view = (UIImageView *)[self viewWithTag:10001];
    [view startAnimating];
}

- (void)stopBAction:(UIButton *)sender
{
    UIImageView *view = (UIImageView *)[self viewWithTag:10001];
    [view stopAnimating];
}







@end
